<?php

/**
 * 特色商品控制器
 *
 * @author yanghj
 * @time 2014-12-29
 */
class TeseApp extends MallbaseApp
{
    private $_feature_mod;
    private $_goods_mod;
    private $_gcategory_mod;
    protected $page_size = 36;

    public function __construct()
    {
        $this->TeseApp();
    }
    
    public function TeseApp()
    {
        parent::__construct();
        
        $this->_feature_mod = m("features");
        $this->_goods_mod = m("goods");
        $this->_gcategory_mod =& bm('gcategory', array('_store_id' => 0));
    }

    public function index()
    {
        $condition = "";
        $cate_id = empty($_GET['cate_id']) ? 0 : intval($_GET['cate_id']);
        //获取有商品的二级分类
        $cates = $this->_gcategory_mod->get_by_cate_ids($this->_get_cate_ids_by_goods());
        $cate_id && $cate_ids = $this->_gcategory_mod->get_descendant_ids($cate_id, true);
        if(isset($cate_ids) && $cate_ids){
            $condition .= " AND cate_id_2 = {$cate_ids[0]}";
            unset($cate_ids[0]);
            $cate_ids && $condition .= " AND cate_id_3 " . db_create_in($cate_ids);
        }

        /* 分页信息 */
        $page = $this->_get_page($this->page_size);
        $feature_list = $this->_feature_mod->find(array(
            'conditions' => 'g.if_show = 1' . $condition,
            'fields' => 'fg.goods_id,fg.title,g.price,default_image',
            'join' => 'has_goods',
            'order' => 'fg.sort ASC, fg.add_time DESC',
            'count' => true,
            'limit' => $page['limit'],
        ));

        foreach ($feature_list as $key => $goods)
        {
            empty($goods['default_image']) && $feature_list[$key]['default_image'] = Conf::get('default_goods_image');

        }
        $page['item_count'] = $this->_feature_mod->getCount();
        $this->_format_page($page);
        $this->assign('page_info', $page); //分页数据
        $this->assign('cates', $cates); //二级分类
        $this->assign('cate_id', $cate_id); //商品列表
        $this->assign('feature_list', $feature_list); //商品列表

        //配置seo信息
        $this->_config_seo($this->_get_seo_info());

        $this->display("feature.index.html");

    }
    
    /*public function index()
    {
        $cate_id = empty($_GET['cate_id']) ? 0 : intval($_GET['cate_id']);
        //获取有商品的二级分类
        $cates = $this->_gcategory_mod->get_by_cate_ids($this->_get_cate_ids_by_goods());
        $feature_list = $this->_get_data_info();
        $this->assign('cates', $cates); //二级分类
        $this->assign('cate_id', $cate_id); //商品列表
        $this->assign('feature_list', $feature_list); //商品列表

        //配置seo信息
        $this->_config_seo($this->_get_seo_info());
        
        $this->display("feature.index.html");
        
    }*/

    public function ajax_data()
    {
        $data=array('done'=>3,'data'=>array());
        $feature_list = $this->_get_data_info();
        if($feature_list)
        {
            $data['done'] = $feature_list<$this->page_size ? 2 : 1;
            $data['data'] = $feature_list;
        }
        else
        {
            $data['done'] = 3;
        }
        echo json_encode($data);
        exit;
    }
    
    private function _get_seo_info()
    {
        $result = array();
        
        return $result;
    }

    private function _get_data_info()
    {
        $data = array();
        $condition = "";
        $cate_id = empty($_GET['cate_id']) ? 0 : intval($_GET['cate_id']);
        $cate_id && $cate_ids = $this->_gcategory_mod->get_descendant_ids($cate_id, true);
        if(isset($cate_ids) && $cate_ids){
            $condition .= " AND cate_id_2 = {$cate_ids[0]}";
            unset($cate_ids[0]);
            $cate_ids && $condition .= " AND cate_id_3 " . db_create_in($cate_ids);
        }

        /* 分页信息 */
        $page = $this->_get_page($this->page_size);
        $feature_list = $this->_feature_mod->find(array(
            'conditions' => 'g.if_show = 1' . $condition,
            'fields' => 'fg.goods_id,fg.title,g.price,default_image',
            'join' => 'has_goods',
            'order' => 'fg.sort ASC, fg.add_time DESC',
            'count' => true,
            'limit' => $page['limit'],
        ));

        $i = 0;
        foreach ($feature_list as $key => $goods)
        {
            $data[$i]=$feature_list[$key];
            empty($goods['default_image']) && $data[$i]['default_image'] = Conf::get('default_goods_image');
            $i ++;
        }

        return $data;
    }
    
    private function _get_cate_ids_by_goods()
    {
        $result = array();
        $goodses = $this->_goods_mod->find(array(
            'conditions' => 'if_show = 1 AND is_feature = 1',
            'fields' => 'distinct cate_id_2',
        ));

        foreach ($goodses as $goods)
        {
            $result[] = $goods['cate_id_2'];
        }

        return $result;
    }

}
